package com.ksd.common.web.threadlocal;



import com.ksd.common.web.model.SysUser;

import java.util.Objects;

/**
 * @author 飞哥yykk
 * 更多学习关注飞哥B站
 * 地址是：https://space.bilibili.com/490711252
 * @title: UserThrealLocal
 * @projectName ksd-user-course-center
 * @description: TODO
 * @date 2021/9/2721:51
 */
public class UserIdThrealLocal {

    private static InheritableThreadLocal<SysUser> threadLocal = new InheritableThreadLocal<>();

    /**
     * 设置当前线程的用户ID
     *
     * @param userId
     */
    public static void set(SysUser userId) {
        threadLocal.set(userId);
    }

    /**
     * 获取当前线程的用户ID
     *
     * @return
     */
    public static SysUser get() {
        SysUser sysUsers = threadLocal.get();
        if (Objects.isNull(sysUsers)) {
            return new SysUser();
        }
        return sysUsers;
    }

    /**
     * 删除当前线程缓存的用户ID
     *
     * @return
     */
    public static void remove() {
       threadLocal.remove();
    }

}
